<?php
//对{field } ... {/field }的解析
function smarty_block_field(&$params, $content, &$smarty, &$repeat){
   return $smarty->callblockfunc($params,$content,$smarty,$repeat, getfield);
}

/**
 * 参数：
 * 	bc: bc的名称
 *  field: 字段名称
 * @param unknown_type $params
 */
function getfield(&$params) {
	global $_db, $cf_updatecache, $cf_pagenum, $cf_fieldauthflg;
	
	if(!isset($_SESSION['_field_lastupdated'])) $_SESSION['_field_lastupdated'] = 0;
	$lastupdated = &$_SESSION['_field_lastupdated'];
	
	//#region 缓存所有字段
	if( (time()-$lastupdated>$cf_updatecache ) || !isset($_SESSION['_fields'])){
		unset($_SESSION['_fields']);
		
		//对于超级用户或没有设置字段级权限,满足其所有权限
		if('-1'==@$_SESSION['userid'] || !$cf_fieldauthflg){
			$sql = "select t.* from t_field t";
		}else{
			$sql = "select distinct t.* from t_field t
				left join t_rolefield rv on rv.c_fieldid=t.c_id 
				left join t_userrole ur on ur.c_roleid=rv.c_roleid 
				left join t_user u on ur.c_userid=u.c_id where 1=1 and u.c_id='$_SESSION[userid]'";
		}
		
		$sql .= " order by c_updated desc";
		
		//获取结果集
		$res = $_db->query($sql);
		$_SESSION['_fields'] = array();
		foreach ($res as $row){
			//$row = array("id"=>$row['c_id'], "bc"=>$row['c_bc'], "field"=>$row['c_field'], "desc"=>$row['c_desc']);
			$_SESSION['_fields'][$row['bc']][$row['field']] = $row;
			$_SESSION['_fields'][0][] = $row;
		}
		
		$_SESSION['_field_lastupdated'] = time();
		//error_log(print_r($_SESSION['_fields']['order'],1));
	}
	//#endreg 缓存所有字段
	
	//#region 获取筛选结果集
	$res = array();
	if(array_key_exists('id', $params)){
		foreach ($_SESSION['_fields'][0] as $field){
			if($params['id'] == $field['id']){
				$res = array($field);
				break;
			}
		}
	}
	elseif(!isempty(@$params['bc']) && !isempty(@$params['field'])){
		$field = @$_SESSION['_fields'][$params['bc']][$params['field']];
		if(null==$field) ;
		else
			$res = array($field);
	}
	else if(!isempty(@$params['bc'])){
		$field = @$_SESSION['_fields'][$params['bc']];
		if(null==$field) ;
		else{
			$res = array();
			foreach ($field as $fname => $fall){
				$res[] = $fall;
			}
		}
	}
	else{
		$res = $_SESSION['_fields'][0];
	}
	//#endreg 获取筛选结果集
		
	//#region 分页
	$params['totalcount'] = count($res);
	$params['pageindex'] = (!isset($params['pageindex']) || $params['pageindex']<0)?0:(int)$params['pageindex'];
	if(!array_key_exists('pagenum', $params)){
		$params['pagenum'] = $cf_pagenum;
	}
	if(0>=$params['pagenum']){
		$params['pagenum'] = $params['totalcount'];
	}
	else{
		$start = $params['pagenum']*$params['pageindex'];
		$end = $start + $params['pagenum'];
		$tmp = array();
		
		$i = 0;
		$end = $end < count($res) ? $end : count($res);
		for($i=$start; $i<$end; $i++){
			$tmp[] = $res[$i];
		}
		$res = $tmp;
	}
	//#endreg 分页
	
	return $res;
}

/**
 * 获取可以访问的bc的字段, 用于select中. 所有的字段都以t.c_开头, 自己写的sql中注意配合
 * 
 * @param unknown_type $bc bc名
 */
/*
function getselectlist($bc){
	global $cf_fieldauthflg;
	
	if(empty($bc)) return null;
	
	//如果没有开启字段级权限控制
	if(!$cf_fieldauthflg) 
		return "t.*";
		
	//如果开启字段级权限控制
	$id = "t.c_id";
	$selectlist = $id;
	$params = array("bc"=>$bc);
	$fields = getfield($params);
	foreach ($fields as $index=>$field){
		if(is_numeric($index) && !empty($field[field])){
			$selectlist .= ",t.c_$field[field]";
		}
	}
	if(strlen($selectlist)<=strlen($id)) return null;
	return $selectlist;
}
//*/
?>